from datetime import datetime

from django.shortcuts import render_to_response, render

from accounts.models import User
from mall.models import Product
from system.models import Slider, News
from utils import contants


def index(request):
    '''首页'''
    # print('request.my_user:', request.my_user)
    # 查询轮播图
    slider_list = Slider.objects.filter(types=contants.SLEDER_TYPE_INDEX)

    # 首页新闻渲染
    now_time = datetime.now()
    news_list = News.objects.filter(types=contants.NEWS_TYPE_NEW, is_top=True, is_valid=True,
                                    start_time__lte=now_time,
                                    end_time__gte=now_time,
                                    )

    # 精选推荐
    jx_list = Product.objects.filter(status=contants.PRODUCT_STATUS_SELL,
                                     is_valid=True,
                                     tags__code='jxtj')
    # 酒水推荐
    js_list = Product.objects.filter(status=contants.PRODUCT_STATUS_SELL,
                                     is_valid=True,
                                     tags__code='jstj')
    # 从session中获取用户id
    # user_id = request.session[contants.LOGIN_SESSION_ID]
    # print(user_id)
    # # 查询当前登录的用户
    # user = User.objects.get(pk=user_id)
    return render(request, 'index.html', {
        'slider_list': slider_list,
        'news_list': news_list,
        # 'user': user,
        'jx_list': jx_list,
        'js_list': js_list,
    }
                  )













